package com.x.provider.customer.controller.app;

import com.x.core.utils.BeanUtil;
import com.x.core.web.api.R;
import com.x.core.web.controller.BaseFrontendController;
import com.x.core.web.page.PageList;
import com.x.provider.api.customer.enums.CustomerRelationEnum;
import com.x.provider.api.customer.model.dto.common.SetEntityAddressReqDTO;
import com.x.provider.api.mc.model.dto.SendVerificationCodeDTO;
import com.x.provider.api.mc.service.SmsRpcService;
import com.x.provider.api.oss.service.OssRpcService;
import com.x.provider.customer.factory.dto.CustomerDTOFactory;
import com.x.provider.customer.factory.vo.CustomerVOFactory;
import com.x.provider.customer.model.ao.*;
import com.x.provider.customer.model.bo.ExternalAuthBO;
import com.x.provider.customer.model.domain.customer.CustomerRelation;
import com.x.provider.customer.model.vo.CustomerRelationVO;
import com.x.provider.customer.model.vo.SetCustomerAddressRequestVO;
import com.x.provider.customer.model.vo.SimpleCustomerVO;
import com.x.provider.customer.service.common.EntityAddressService;
import com.x.provider.customer.service.customer.AuthenticationService;
import com.x.provider.customer.service.customer.CustomerRelationService;
import com.x.provider.customer.service.customer.CustomerService;
import com.x.provider.customer.service.customer.CustomerStatService;
import com.x.provider.customer.service.customer.authext.ExternalAuthEngine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "用户服务")
@Slf4j
@RestController
@RequestMapping("/app/customer")
public class CustomerController extends BaseFrontendController {

//    private final CustomerService customerService;
//    private final CustomerRelationService customerRelationService;
//    private final OssRpcService ossRpcService;
//    private final SmsRpcService smsRpcService;
//    private final CustomerStatService customerStatService;
//    private final AuthenticationService authenticationService;
//    private final ExternalAuthEngine externalAuthEngine;
//    private final CustomerDTOFactory customerFactory;
//    private final CustomerVOFactory customerVOFactory;
//    private final EntityAddressService customerAddressService;
//
//    public CustomerController(CustomerService customerService,
//                              CustomerRelationService customerRelationService,
//                              OssRpcService ossRpcService,
//                              SmsRpcService smsRpcService,
//                              CustomerStatService customerStatService,
//                              AuthenticationService authenticationService,
//                              ExternalAuthEngine externalAuthEngine,
//                              CustomerDTOFactory customerFactory,
//                              CustomerVOFactory customerVOFactory,
//                              EntityAddressService customerAddressService){
//        this.customerService = customerService;
//        this.customerRelationService = customerRelationService;
//        this.ossRpcService = ossRpcService;
//        this.smsRpcService = smsRpcService;
//        this.customerStatService = customerStatService;
//        this.authenticationService = authenticationService;
//        this.externalAuthEngine = externalAuthEngine;
//        this.customerFactory = customerFactory;
//        this.customerVOFactory = customerVOFactory;
//        this.customerAddressService = customerAddressService;
//    }
//
//    @ApiOperation(value = "用户名密码注册")
//    @PostMapping("/register")
//    public R<Void> register(@RequestBody @Validated UserNamePasswordRegisterAO userNamePasswordRegisterAO){
//        customerService.register(userNamePasswordRegisterAO);
//        return R.ok();
//    }
//
//    @ApiOperation(value = "根据密码登陆,返回token,下次方法其它接口是在此Token至于http header Authorization 中，值为 Bear token")
//    @PostMapping("/login/by/password")
//    public R<String> loginByPassword(@RequestBody @Validated LoginByPasswordAO userNamePasswordLoginAO){
//        return R.ok(customerService.loginByPassword(userNamePasswordLoginAO));
//    }
//
//    @ApiOperation(value = "根据短信验证码登陆或者注册, 返回token,下次方法其它接口是在此Token至于http header Authorization 中，值为 Bear token")
//    @PostMapping("/login/register/by/sms")
//    public R<String> loginOrRegisterBySms(@RequestBody @Validated LoginOrRegBySmsAO loginOrRegBySmsAO){
//        return R.ok(customerService.loginOrRegisterBySms(loginOrRegBySmsAO));
//    }
//
//    @ApiOperation(value = "发送短信验证码")
//    @PostMapping("/sms/verification/code/send")
//    public R<Void> loginOrRegisterBySms(@RequestBody @Validated SendSmsVerificationCodeAO sendSmsVerificationCodeAO){
//        return smsRpcService.sendVerificationCode(SendVerificationCodeDTO.builder().phoneNumber(sendSmsVerificationCodeAO.getPhoneNumber()).build());
//    }
//
//    @ApiOperation(value = "第三方登陆，目前支持微信小程序, 返回token,下次方法其它接口是在此Token至于http header Authorization 中，值为 Bear token")
//    @PostMapping("/login/external")
//    public R<String> login(@RequestBody @Validated ExternalAuthBO externalAuth){
//        return R.ok(externalAuthEngine.authenticate(externalAuth));
//    }
//
//    @ApiOperation("验证手机是否被绑定且发验证码")
//    @PostMapping("/phone/bind/validate")
//    public R<Void> bindPhone(@RequestBody @Validated ValidatePhoneAO validatePhoneAO) {
//        customerService.checkPhoneBound(getCurrentCustomerId(), validatePhoneAO);
//        return R.ok();
//    }
//
//    @ApiOperation("绑定手机")
//    @PostMapping("/phone/bind")
//    public R<Void> bindPhone(@RequestBody @Validated BindPhoneAO bindPhoneAO) {
//        customerService.bindPhone(getCurrentCustomerId(), bindPhoneAO);
//        return R.ok();
//    }
//
//    @ApiOperation(value = "修改密码")
//    @PostMapping("/password/change")
//    public R<Void> changePassword(@RequestBody @Validated ChangePasswordByOldPasswordAO changePasswordByOldPasswordAO){
//        customerService.changePassword(getCurrentCustomerId(), changePasswordByOldPasswordAO);
//        return R.ok();
//    }
//
//    @ApiOperation(value = "更改手机号码")
//    @PostMapping("/phone/change")
//    public R<Void> changePhone(@RequestBody @Validated ChangePhoneAO changePhoneAO){
//        customerService.changePhone(getCurrentCustomerId(), changePhoneAO);
//        return R.ok();
//    }
//
//
//    @ApiOperation(value = "关注,取消关注")
//    @PostMapping("/follow/or/unfollow")
//    public R<CustomerRelationVO> following(@RequestBody FollowOrUnFollowAO followOrUnFollowAO){
//        CustomerRelation customerRelation = customerRelationService.changeCustomerRelation(getCurrentCustomerId(), followOrUnFollowAO.getToCustomerId(), followOrUnFollowAO.getFollow());
//        return R.ok(BeanUtil.prepare(customerRelation, CustomerRelationVO.class));
//    }
//
//    @ApiOperation(value = "查询关注列表")
//    @GetMapping("/relation/list")
//    public R<PageList<SimpleCustomerVO>> listCustomerRelation(@RequestParam Long cursor,
//                                                              @RequestParam Integer pageSize,
//                                                              @RequestParam @Validated @Min(0)  @ApiParam(value = "用户id") long customerId,
//                                                              @RequestParam @Validated @Min(1)  @ApiParam(value = "用户关系 0 没有关系 1 关注关系 2 朋友关系 3 粉丝关系 ")
//                                                                          Integer customerRelation){
//        if (CustomerRelationEnum.FRIEND.getValue() == customerRelation.intValue()){
//            customerId = getCurrentCustomerId();
//        }
//        PageList<CustomerRelation> relationList = customerRelationService.listCustomerRelation(customerId, CustomerRelationEnum.valueOf(customerRelation), getPageDomain());
//        if (relationList.getList().isEmpty()){
//            return R.ok(new PageList<>());
//        }
//        List<Long> relationCustomerIdList = customerRelation.equals(CustomerRelationEnum.FANS.getValue()) ? relationList.getList().stream().map(item -> item.getFromCustomerId()).collect(Collectors.toList()) :
//                relationList.getList().stream().map(item -> item.getToCustomerId()).collect(Collectors.toList());
//        return R.ok(PageList.map(relationList, customerVOFactory.prepare(relationCustomerIdList, getCurrentCustomerIdAndNotCheckLogin())));
//    }

//    @ApiOperation(value = "设置用户地址")
//    @PostMapping("/address/set")
//    public R<Void> setCustomerAddress(@RequestBody @Validated SetCustomerAddressRequestVO setCustomerAddressRequestVO){
//        SetEntityAddressReqDTO setCustomerAddressRequestBO = BeanUtil.prepare(setCustomerAddressRequestVO, SetEntityAddressReqDTO.class);
//        setCustomerAddressRequestBO.setEntityId(getCurrentCustomerId());
//        customerAddressService.setEntityAddress(setCustomerAddressRequestBO);
//        return R.ok();
//    }

}
